<?php

class Trackcomments_model extends Base_Model
{
    const TABLE = 'mod_track_comments';

    public function __construct()
    {
        $this->load->database();
    }

    /**
     * 获取评论列表信息
     *
     * @param $where
     * @param $offset
     * @param $limit
     * @return mixed
     */
    public function getCommentsList($whereData, $offset, $limit)
    {
        $where = ' where 1';
        if(isset($whereData['uid'])) $where .= ' AND a.uid = '.$whereData['uid'];
        if($whereData['iscomment'] == 1){
            $where .= ' AND b.id is not null ';
        }else{
            $where .= ' AND b.id is null ';
        }
        $limit = ' limit '.$offset.', '.$limit;

        $sql = <<<EOT
SELECT
	a.id,
	a.title,
	c.thumb,
	b.guider_score,
	b.created,
	b.comments
FROM
	app_order a
LEFT JOIN (
	SELECT
		id,
		uid,
		`name`,
		thumb,
		'ACTIVITY' type
	FROM
		mod_activity
	UNION ALL
		SELECT
			id,
			uid,
			`name`,
			thumb,
			'TRACK' type
		FROM
			mod_track
	UNION ALL
		SELECT
			id,
			uid,
			`name`,
			thumb,
			'TRACK' type
		FROM
			mod_ziying
) c ON c.id = a.goodsId
AND c.type = a.goodsType
LEFT JOIN mod_track_comments b ON a.id = b.oid
EOT;
        $sql .= $where.$limit;
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    /**
     * 获取指定评论
     *
     * @param $where
     * @return array
     */
    public function getComments($where)
    {
        return $this->db->get_where(self::TABLE, $where)->row_array();

    }

    /**
     * 添加评论
     *
     * @param $data
     */
    public function addComments($data)
    {
        return $this->db->insert(self::TABLE, $data);
    }

    
}